<template>
  <div class="conMain">
    <el-card class="box-card">
      <div slot="header" class="topfix">
        <div style="text-align: right;">
          <el-button v-has="'site/payments/add'" type="primary" size="small" class="ml10" plain @click="add"><i
            class="el-icon-plus"
          /> 添加参数</el-button>
        </div>
      </div>

      <el-table v-loading="loading" :data="list" style="width: 100%" :empty-text="emptytxt">
        <el-table-column prop="id" label="序号" align="center" width="80" />
        <el-table-column prop="name" label="支付商户名称" :show-overflow-tooltip="true" align="center" />

        <el-table-column label="支付类型" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 1" type="success" size="mini" disable-transitions>支付宝</el-tag>
            <el-tag v-else-if="scope.row.type == 3" size="mini" disable-transitions>微信</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createtime" label="创建时间" align="center" min-width="110" />

        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-link v-has="'site/payments/edit'" :underline="false" type="primary" title="编辑" @click="edit(scope.row)">
              编辑
            </el-link>
          </template>
        </el-table-column>
      </el-table>

    </el-card>

    <el-dialog
      :title="sub.id==0?'添加参数信息':'编辑参数信息'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      center
      width="40%"
    >
      <el-form ref="addRuleFormRef" :model="sub" :rules="rules" label-width="110px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="sub.name" clearable />
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="sub.type" size="mini">
            <el-radio v-for="item in typelist" :key="item.val" :label="item.val">{{ item.txt }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="应用ID" prop="app_id">
          <el-input v-model="sub.app_id" clearable />
        </el-form-item>

        <el-form-item label="秘钥" prop="secret">
          <el-input v-model="sub.secret" clearable autosize type="textarea" />
        </el-form-item>

        <el-form-item v-if="sub.type === '3'" label="支付商户号" prop="mch_id">
          <el-input v-model="sub.mch_id" clearable />
        </el-form-item>

        <el-form-item v-if="sub.type === '3'" label="支付秘钥" prop="mch_secret">
          <el-input v-model="sub.mch_secret" clearable />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subForm('addRuleFormRef')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  payments_index,
  payments_edit_post,
  payments_add_post
} from '@/api/site'

export default {
  data() {
    return {
      loading: true,
      emptytxt: '',
      list: [],
      params: {
        page: 1,
        pagesize: 10
      },
      typelist: [{
        val: '1',
        txt: '支付宝'
      }, {
        val: '3',
        txt: '微信'
      }],
      sub: {
        id: 0,
        type: '1',
        app_id: '',
        secret: '',
        mch_id: ''
      },
      dialogVisible: false,
      rules: {
        name: [{
          required: true,
          message: '参数名称不能为空',
          trigger: 'blur'
        }],
        type: [{
          required: true,
          message: '请选择支付参数类型',
          trigger: 'blur'
        }],
        app_id: [{
          required: true,
          message: '应用ID不能为空',
          trigger: 'blur'
        }],
        secret: [{
          required: true,
          message: '秘钥不能为空',
          trigger: 'blur'
        }],
        mch_id: [{
          required: true,
          message: '微信支付商户号不能为空',
          trigger: 'blur'
        }],
        mch_secret: [{
          required: true,
          message: '微信支付秘钥不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.getPaymentsList()
  },
  methods: {
    getPaymentsList(page) {
      this.params.page = page || this.params.page
      this.emptytxt = '加载中.'
      this.loadig = true
      payments_index(this.query).then(res => {
        this.list = res.data.list
        this.total = res.data.total
        this.loading = false
        if (this.list.length === 0) {
          this.emptytxt = '暂无数据'
        }
      })
    },
    add() {
      this.sub = {
        id: 0,
        type: '1',
        app_id: '',
        secret: '',
        mch_id: ''
      }
      this.dialogVisible = true
    },
    subForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.sub.id > 0) {
            delete this.sub.createtime
            payments_edit_post(this.sub).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getPaymentsList()
            })
          } else {
            payments_add_post(this.sub).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getPaymentsList()
            })
          }
        } else {
          return false
        }
      })
    },
    edit(row) {
      this.$nextTick(() => {
        this.sub = row
        this.sub.type = row.type + ''
        this.dialogVisible = true
      })
    }
  }
}
</script>

<style>
</style>
